﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Spring.Context;
using Spring.Context.Support;
using SpringNH.Impl;
using SpringNH.Model;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            UserDao dao = ctx.GetObject("UserDao") as UserDao;

            MyUsers newUser = null;
            //try
            //{
            //    newUser = dao.Load("1211");
            //}
            //catch
            //{
            //}

            if (newUser == null)
            {
                newUser = new MyUsers();
                //newUser.LogonId = System.Guid.NewGuid().ToString();
                newUser.UserName = System.DateTime.Now.ToLongDateString();
                newUser.Password = System.DateTime.Now.ToLongTimeString();
                newUser.EmailAddress = System.DateTime.Now.Month.ToString();
                newUser.LastLogon = DateTime.Now;

                // Tell NHibernate that this object should be saved
                dao.Save(newUser);
            }



            //MyUsers joeCool = dao.Load("1211");

            //// set Joe Cool's Last Login property
            //joeCool.LastLogon = DateTime.Now;

            // flush the changes from the Session to the Database
            //dao.UpdateObject(joeCool);

            IList recentUsers = dao.GetAllObjectsList();
            //foreach (MyUsers user in recentUsers)
            //{
            //    //Assert.IsTrue(user.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) ); 
            //    Console.WriteLine(user.UserName);
            //    Console.WriteLine(user.Password);
            //    Console.WriteLine(user.LastLogon);
            //}
            //Console.ReadLine();//让程序停留，回车关闭。

            this.GridView1.DataSource = recentUsers;
            this.GridView1.DataBind();
        }
    }
}
